<?php
namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Cat;



class CatController extends Controller {

    //栏目列表
    public function catlist(){
        $catlist=Cat::get();
        return view('admin.catslist',['catlist'=>$catlist]);
    }


    //删除栏目
    public function catdel($cat_id){
           return Cat::where('cat_id',$cat_id)->delete() ? '删除成功!' : '删除失败!';
}

    //增加栏目
   public function catadd(request $req){
       if($req->isMethod('post')){
           $cats=new Cat();
           $cats->cat_name=$_POST['cat_name'];
           $cats->parent_id=$_POST['parent_id'];
           $cats->cat_intro=$_POST['cat_intro'];
           /*var_dump($cats->cat_name);
           var_dump($cats->parent_id);
           var_dump($cats->cat_intro);*/
           //$cats->save();

       }

       $cats=new Cat();
       //var_dump($cats);
       $catlist=$cats->get();
       $cat=$cats->getTree($catlist);
       var_dump($cat);


       return view('admin.catsadd',['cat'=>$cat]);
              }






    //修改栏目
    public function catedit(request $req,$id){
        if(empty($_POST)){
            $catfisrt=Cat::where('cat_id',$id)->first();
            $cats=Cat::get();
            //print_r($catfisrt);
            return view('admin/catsedit',['catone'=>$catfisrt],['cat'=>$cats]);
        }else{
            $catedit=Cat::where('cat_id',$id)->first();
            $catedit->cat_name=$req->input('cat_name');
            $catedit->parent_id=$req->input('parent_id');
            $catedit->cat_intro=$req->input('cat_intro');

            $catedit->save();

        }

    }




   

}
